import {Component, OnInit} from '@angular/core';
import {DynamicComponent} from '../../../../../../ng-tools';
import {UserService} from '../../../../../services/api/user.service';
import {AbstractControl, FormBuilder, FormGroup, Validators} from '@angular/forms';
import {Router} from '@angular/router';
import {timer} from 'rxjs';
import * as CryptoJS from 'crypto-js';
import {passwordWeak} from '../../user/user-password';

export function passwordGroupValidator(group: AbstractControl): any {
  const p1 = group.get('newPassword').value;
  const p2 = group.get('newPasswordCheck').value;
  if (p1 !== null && p2 !== null) {
    if (p1.length < 8 || p1.length > 16) {
      return '密码位数不正确';
    }
    if (passwordWeak.indexOf(p1) >= 0) {
      return '密码为弱类型';
    }
  }
  return p1 !== p2 ? '密码不匹配' : null;
}

export class User {
  password: string;
  newPassword: string;

  constructor() {
  }

  public fromForm(form: FormGroup): User {
    this.password = form.get('password').value;
    this.newPassword = form.get('passwordGroup.newPassword').value;
    return this;
  }

  public md5(): User {
    this.password = CryptoJS.MD5(this.password).toString();
    this.newPassword = CryptoJS.MD5(this.newPassword).toString();
    return this;
  }
}

@Component({
  selector: 'change-password',
  templateUrl: './change-password.component.html',
  styleUrls: ['./change-password.component.scss'],
  providers: [UserService]
})
export class ChangePasswordComponent implements OnInit, DynamicComponent {
  passwordhint = '为了您账户的安全，建议使用复杂密码';
  userForm: FormGroup;
  options: any;
  dynamic: true;
  private user = new User();
  defaultFormValue: any;
  oldPasswordhint = '请输入原密码';

  constructor(private userService: UserService, private formBuilder: FormBuilder, private router: Router) {
    this.userForm = this.formBuilder.group({
      password: ['', Validators.required],
      passwordGroup: this.formBuilder.group({
        newPassword: ['', Validators.required],
        newPasswordCheck: ['', Validators.required]
      }, {validator: passwordGroupValidator}),
    });
    this.defaultFormValue = {
      password: '',
      passwordGroup: this.formBuilder.group({
        newPassword: '',
        newPasswordCheck: ''
      }),
    };
    this.userForm.patchValue(this.defaultFormValue);
  }

  ngOnInit() {
  }

  changePassword() {
    if (this.userForm.valid) {
      this.user = this.user.fromForm(this.userForm).md5();
      this.userService.post(this.userService.urls.changePassword, this.user).subscribe(() => {
        this.userForm.reset();
        this.signOut();
      }, (resp1) => {
        this.userForm.reset();
        this.oldPasswordhint = resp1.error.msg;
        timer(3000).subscribe(() => {
          this.oldPasswordhint = '请输入原密码';
        });
      });
    }
  }

  signOut() {
    this.userService.get(this.userService.urls.signOut).subscribe(() => {
      this.router.navigateByUrl('/login').catch();
    });
  }

  checkLength(event: any) {
    if (event) {
      if (event.length < 8 || event.length > 16) {
        this.passwordhint = '请输入8到16位密码';
      } else {
        if (passwordWeak.indexOf(event) >= 0) {
          this.passwordhint = '您输入的密码为弱类型';
        } else {
          this.passwordhint = '为了您账户的安全，建议使用复杂密码';
        }
      }
    }
  }

  enter(event) {
    if (event.keyCode === 13) {
      this.changePassword();
    }
  }
}
